import { Action, Actor } from 'iflux2';
import { SectionTypes } from '../constants/actionTypes'
export default class SectionActor extends Actor {
    defaultState () {
        return {
            sections:[]
        }
    }
    /**
     * 获取文章栏目列表
     * @param {*} state 
     * @param {*} list 
     */
    @Action(SectionTypes.FETCH_SECTIONS)
    fetchSections(state,list){
        return state.set('sections',list);
    }

    /**
     * 添加文章栏目
     * @param {*} state 
     * @param {*} section 
     */
    @Action(SectionTypes.ADD_SECTION)
    addSection(state,section){
        return state.update('sections',sections=>sections.push(section))
    }

    /**
     * 修改文章栏目
     * @param {*} state 
     * @param {*} section 
     */
    @Action(SectionTypes.UPDATE_SECTION)
    updateSection(state,section){
        return state.update('sections',sections => {
            return sections.map(item=>{
                if(item.get('id') === section.get('id')){
                    return item.merge(section)
                }
                return item;
            })
        })
    }
    /**
     * 删除文章栏目
     * @param {*} state 
     * @param {*} id 
     */
    @Action(SectionTypes.DELETE_SECTION)
    deleteSection(state,id){
        return state.update('sections',sections=>sections.filter(item=>item.get('id') !== id))
    }
}